package com.ruoyi.userr.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.userr.domain.UserFollow;
import com.ruoyi.userr.service.IUserFollowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户关注关系Controller
 * 
 * @author sr
 * @date 2025-08-15
 */
@RestController
@RequestMapping("/userr/follow")
public class UserFollowController extends BaseController
{
    @Resource
    private IUserFollowService userFollowService;

    /**
     * 查询用户关注关系列表
     */
    //@PreAuthorize("@ss.hasPermi('userr:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserFollow userFollow)
    {
        startPage();
        List<UserFollow> list = userFollowService.selectUserFollowList(userFollow);
        return getDataTable(list);
    }

    /**
     * 导出用户关注关系列表
     */
    @PreAuthorize("@ss.hasPermi('userr:follow:export')")
    @Log(title = "用户关注关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserFollow userFollow)
    {
        List<UserFollow> list = userFollowService.selectUserFollowList(userFollow);
        ExcelUtil<UserFollow> util = new ExcelUtil<UserFollow>(UserFollow.class);
        util.exportExcel(response, list, "用户关注关系数据");
    }

    /**
     * 获取用户关注关系详细信息
     */
    //@PreAuthorize("@ss.hasPermi('userr:follow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(userFollowService.selectUserFollowById(id));
    }

    /**
     * 新增用户关注关系
     */
    //@PreAuthorize("@ss.hasPermi('userr:follow:add')")
    @Log(title = "用户关注关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserFollow userFollow)
    {
        return toAjax(userFollowService.insertUserFollow(userFollow));
    }

    /**
     * 修改用户关注关系
     */
   // @PreAuthorize("@ss.hasPermi('userr:follow:edit')")
    @Log(title = "用户关注关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserFollow userFollow)
    {
        return toAjax(userFollowService.updateUserFollow(userFollow));
    }

    /**
     * 删除用户关注关系
     */
   // @PreAuthorize("@ss.hasPermi('userr:follow:remove')")
    @Log(title = "用户关注关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(userFollowService.deleteUserFollowByIds(ids));
    }

    /**
     * 根据关注者ID查询其关注的所有用户
     * @param followerId 关注者ID（如902）
     * @return 关注列表（包含被关注者ID、姓名、头像、性别、加入时间）
     */
    @GetMapping("/list/{followerId}")
    public AjaxResult getFollowedUsers(@PathVariable("followerId") Long followerId) {
        if (followerId == null) {
            return AjaxResult.error("关注者ID不能为空");
        }
        List<UserFollow> followedUsers = userFollowService.getFollowedUsers(followerId);
        return AjaxResult.success(followedUsers);
    }

    /**
     * 获取指定用户的关注个数
     * @param followerId 关注者ID
     * @return 关注数量（JSON格式）
     */
    @GetMapping("/count/{followerId}")
    public AjaxResult getFollowCount(@PathVariable("followerId") Long followerId) {
        Long count = userFollowService.getFollowCount(followerId);
        return AjaxResult.success("关注个数", count);
    }

    /**
     * 根据被关注者ID查询粉丝个数
     * @param followedId 被关注者ID（目标用户）
     * @return 粉丝数量
     */
    @GetMapping("/fan-count/{followedId}")
    public AjaxResult getFanCount(@PathVariable("followedId") Long followedId) {
        Long fanCount = userFollowService.getFanCount(followedId);
        return AjaxResult.success("粉丝个数", fanCount);
    }


    /**
     * 根据被关注者ID查询所有粉丝列表
     * @param followedId 被关注者ID（目标用户）
     * @return 粉丝列表（包含粉丝ID、昵称、头像等详情）
     */
    @GetMapping("/followers/{followedId}")
    public AjaxResult getFollowerList(@PathVariable("followedId") Long followedId) {
        List<UserFollow> followerList = userFollowService.getFollowerList(followedId);
        return AjaxResult.success(followerList);
    }




}
